﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Bizu.Models;
using Newtonsoft.Json;

namespace Bizu.Controllers
{
    public class PostController : Controller
    {
        //
        // GET: /Post/

        BizuEntities db = new BizuEntities();

        public ActionResult Index(int id = 1)
        {
            var q = (from p in db.bizu_Post where p.id == id select p).FirstOrDefault();

            ViewBag.idPost = q.id;
            ViewBag.titlePost = q.title;
            ViewBag.ContentPost = q.content_post;
            ViewBag.datePost = q.date_post;
            ViewBag.countComment = (from c in db.bizu_Comment where c.post_id == id select c).Count();

            return View();
        }

        [HttpPost]
        [ValidateInput(false)]
        public void addComment(bizu_Comment newComment)
        {
            int userID = int.Parse(Request.Form["userID"]);
            string contentComment = Request.Form["contentComment"];
            int postID = int.Parse(Request.Form["postID"]);

            newComment.user_id = userID;
            newComment.content_comment = modul.modulPost.convertPostContent(contentComment);
            newComment.post_id = postID;

            db.bizu_Comment.Add(newComment);
            db.SaveChanges();
        }

        public string getComment(int postID = 1)
        {
            var q = (from c in db.bizu_Comment
                     where c.post_id == postID
                     select new
                     {
                         contentComment = c.content_comment,
                     }).ToList();

            return JsonConvert.SerializeObject(q);
        }

    }
}
